<?
//---------------------------Set Category Order when Insert--------------------------//           
  function setcatorderinsert($tablename="",$categoryid="",$categoryorder="",$orderno="",$lastorderno="")		  {
		   
		   $orderno = $orderno > $lastorderno ? $lastorderno : $orderno;   
	   
			$sql_ordering = "select $categoryid, $categoryorder from $tablename where $categoryorder >= '$orderno' order by $categoryorder asc";
			$sql_result = mysql_query($sql_ordering);

			$i=1;
			while($row_result=mysql_fetch_array($sql_result))
			{

			$new_category_order = $orderno + $i ;
	     	$sql_update_order = "update $tablename set $categoryorder = '$new_category_order' where $categoryid = '$row_result[$categoryid]'";
			mysql_query($sql_update_order);
			$i++; 

			}
      }
  
//-------------------------------------------------------------------------------------------------//


//---------------------------Set Category Order when Update--------------------------// 
function setcatorderupdate($tablename="",$categoryid="",$categoryorder="",$orderno="",$lastorderno="",$categoryidval="")		      
	   {
			$sql_last_ordering = "select * from $tablename where $categoryorder = '$orderno'";
			$last_cat_id = mysql_result(mysql_query($sql_last_ordering),0,0);

			$sql_upt_order = "UPDATE $tablename SET $categoryorder='$orderno'  WHERE $categoryid = '$categoryidval'";
			mysql_query($sql_upt_order) or die("1".mysql_error());

			$sql_upt_order1 = "UPDATE $tablename SET $categoryorder='$lastorderno'  WHERE $categoryid = '$last_cat_id'";
			mysql_query($sql_upt_order1) or die("2".mysql_error());

         }
 //-------------------------------------------------------------------------------------------------//

//----------------------Set Category Order when Delete--------------------------------------//	
		 
function setcatorderdelete($tablename="",$categoryid="",$categoryorder="",$orderno="",$lastorderno="",$categoryidval="")		   		      
       {  
		   $sql_order = "select $categoryorder from $tablename where $categoryid = '$categoryidval'";
		   $order_result = mysql_query($sql_order);

		   $row_order=mysql_fetch_array($order_result);
		   $category_order = $row_order[$categoryorder];
		   

		   $sql_ordering = "select $categoryid,$categoryorder from $tablename  where $categoryorder > '$category_order' order by $categoryorder asc";
		   $sql_result = mysql_query($sql_ordering);


		   while($row_result=mysql_fetch_array($sql_result))
		   {
			  $old_category_id = $row_result[$categoryid];
			  $old_category_order = $row_result[$categoryorder];
			  $new_category_order = $old_category_order - 1;
			  $sql_update_order = "update $tablename set $categoryorder = '$new_category_order' where $categoryid = '$old_category_id'";
			  mysql_query($sql_update_order);
		   }
		}
   	//----------------------------------------------------------------------------------------------//

 //----------------------Set SubCategory Order when Insert--------------------------------------//
  function  setsubcatorderinsert($tablename="",$subcategoryid="",$subcategoryorder="",$orderno="",$lastorderno="",$categoryid="",$categoryidval="")	
  {
	
	$sql_ordering = "select $subcategoryid, $subcategoryorder from $tablename where $subcategoryorder >= '$orderno' and $categoryid = '$categoryidval' order by $subcategoryorder asc";
	$sql_result = mysql_query($sql_ordering);
    
		while($row_result=mysql_fetch_array($sql_result))
		{
			$old_id = $row_result[$subcategoryid];
			$old_order = $row_result[$subcategoryorder];
			$new_order = $old_order + 1;
			$sql_update_order = "update $tablename set $subcategoryorder = '$new_order' where $subcategoryid = '$old_id' and $categoryid = '$categoryidval'";
			mysql_query($sql_update_order);
		}
  }

//-------------------------------------------------------------------------------------------------//
//----------------------Set SubCategory Order when Update--------------------------------------//
function setsubcatorderupdate($tablename="",$subcategoryid="",$subcategoryidval="",$subcategoryorder="",$orderno="",$lastorderno="",$categoryid="",$categoryidval="")	
		{
		$sql_last_ordering = "select * from $tablename where $subcategoryorder = '$orderno' and $categoryid = $categoryidval";
		$last_cat_id = mysql_result(mysql_query($sql_last_ordering),0,0);

		$sql_upt_order = "UPDATE $tablename SET $subcategoryorder='$orderno'  WHERE $subcategoryid = '$subcategoryidval' and $categoryid = $categoryidval";
		mysql_query($sql_upt_order) or die("1".mysql_error());

		$sql_upt_order1 = "UPDATE $tablename SET $subcategoryorder='$lastorderno'  WHERE $subcategoryid = '$last_cat_id' and $categoryid = $categoryidval";
		mysql_query($sql_upt_order1) or die("2".mysql_error());
		}


//-------------------------------------------------------------------------------------------------//
//------------------------- Set SubCategory Order when Changing Category ------------------------//
function setsubcatorderfixing($tablename="",$subcategoryid="",$categoryid="",$categoryidval="",$subcategoryorder="",$lastorderno="")	
{
	$sql_last_ordering = "select * from $tablename where $subcategoryorder > '$lastorderno' and $categoryid = $categoryidval";
	$rs_last_ordering = mysql_query($sql_last_ordering);
	if(mysql_num_rows($rs_last_ordering)>0){
		while($row_last_ordering=mysql_fetch_array($rs_last_ordering)) {
			$recordid = $row_last_ordering[$subcategoryid];
		
			$sql_upt_order = "UPDATE $tablename SET $subcategoryorder = $subcategoryorder - 1 WHERE $subcategoryid = '$recordid' and $categoryid = $categoryidval";
			mysql_query($sql_upt_order) or die("Ordering Failed".mysql_error());
		}
	}
	mysql_free_result($rs_last_ordering);
}


//-------------------------------------------------------------------------------------------------// 
//----------------------Set SubCategory Order when Delete--------------------------------------// 
 function setsubcatorderdelete($tablename="",$subcategoryid="",$subcategoryidval="",$subcategoryorder="",$orderno="",$lastorderno="",$categoryid="",$categoryidval="")	
   {
	  $sql_order = "select $subcategoryorder from $tablename where $subcategoryid = '$subcategoryidval' and $categoryid = $categoryidval";
	  $order_result = mysql_query($sql_order);

		   $row_order=mysql_fetch_array($order_result);
		   $order_no = $row_order[$subcategoryorder];
		   
		   $sql_ordering = "select $subcategoryid,$categoryid,$subcategoryorder from $tablename  where $subcategoryorder > '$order_no' and $categoryid = $categoryidval order by $subcategoryorder asc";
		   $sql_result = mysql_query($sql_ordering);


		   while($row_result=mysql_fetch_array($sql_result))
		   {
			  $old_id = $row_result[$subcategoryid];
			  $old_order = $row_result[$subcategoryorder];
			  $new_order = $old_order - 1;
			  $sql_update_order = "update $tablename set $subcategoryorder = '$new_order' where $subcategoryid = '$old_id'";
			  //echo "<br>";
			  mysql_query($sql_update_order);
		   }
    }
	
//----------------------Set SubCategory Order when Update When transfer--------------------------------------//
function setsubcatorderupdatetransfer($tablename,$subcategoryordername,$categoryidname,$currentcategoryidval,$subcategoryidname,$subcategoryidval)	
{
	$lastcategoryidval=mysql_result(mysql_query("SELECT $categoryidname FROM $tablename WHERE $subcategoryidname='$subcategoryidval'"),0,0);
					
	if($currentcategoryidval!=$lastcategoryidval)
	{
		$lastsubcategoryorder =mysql_result(mysql_query("SELECT $subcategoryordername FROM $tablename WHERE $subcategoryidname='$subcategoryidval'"),0,0);
		
		$selectsubcatorrderupdate="select * from $tablename where $categoryidname='$lastcategoryidval' and $subcategoryordername>$lastsubcategoryorder order by $subcategoryordername";
		$resultsubcatorrderupdate=mysql_query($selectsubcatorrderupdate);
		while($rowsubcatorrderupdate=mysql_fetch_array($resultsubcatorrderupdate))
		{
			$sql_update_order = "update $tablename set $subcategoryordername = $subcategoryordername-1 where $subcategoryidname = '".$rowsubcatorrderupdate[$subcategoryidname]."'";
			$result_update_order=mysql_query($sql_update_order);
		}
		$subcategoryorder = mysql_result(mysql_query("select ifnull(max($subcategoryordername)+1,1) from $tablename WHERE $categoryidname='$currentcategoryidval'"),0,0);
	}
	else
	{
		$subcategoryorder =mysql_result(mysql_query("SELECT $subcategoryordername FROM $tablename WHERE $subcategoryidname='$subcategoryidval'"),0,0);
	}	
	return $subcategoryorder;
}


//-------------------------------------------------------------------------------------------------//	


// ================================================================================================= //
// 					Function to Re_order all the Display ordering [Start]							 //
// ================================================================================================= //

	function rearrangeOrder($tablename="",$fieldId="",$parentId="",$fieldOrder="",$orderno="",$lastorderno="",$fieldIdVal="",$parent_id="")  
    {  
	   $sql_order = "select $fieldOrder from $tablename where $fieldId = '$fieldIdVal'";
	   $order_result = mysql_query($sql_order);

	   $row_order=mysql_fetch_array($order_result);
	   $category_order = $row_order[$fieldOrder];
	   
		if($parentId != "" && $parent_id != "")
		{
	   		$sql_ordering = "select $fieldId,$fieldOrder 
								from $tablename  
								where $fieldOrder > '$category_order' 
								and $parentId = '$parent_id'
								order by $fieldOrder asc";
		}
		else
		{
			$sql_ordering = "select $fieldId,$fieldOrder 
								from $tablename  
								where $fieldOrder > '$category_order' 
								order by $fieldOrder asc";
		}
	   $sql_result = mysql_query($sql_ordering);


	   while($row_result=mysql_fetch_array($sql_result))
	   {
		  $old_category_id = $row_result[$fieldId];
		  $old_category_order = $row_result[$fieldOrder];
		  $new_category_order = $old_category_order - 1;
		  $sql_update_order = "update $tablename set $fieldOrder = '$new_category_order' where $fieldId = '$old_category_id'";
		  mysql_query($sql_update_order);
	   }
	}

// ================================================================================================= //
// 					Function to Re_order all the Display ordering [End]								 //
// ================================================================================================= //


?>